/**************************************************************************
  This is a library for several Adafruit displays based on ST77* drivers.

  Works with the Adafruit 1.8" TFT Breakout w/SD card
    ----> http://www.adafruit.com/products/358
  The 1.8" TFT shield
    ----> https://www.adafruit.com/product/802
  The 1.44" TFT breakout
    ----> https://www.adafruit.com/product/2088
  The 1.14" TFT breakout
  ----> https://www.adafruit.com/product/4383
  The 1.3" TFT breakout
  ----> https://www.adafruit.com/product/4313
  The 1.54" TFT breakout
    ----> https://www.adafruit.com/product/3787
  The 1.69" TFT breakout
    ----> https://www.adafruit.com/product/5206
  The 2.0" TFT breakout
    ----> https://www.adafruit.com/product/4311
  as well as Adafruit raw 1.8" TFT display
    ----> http://www.adafruit.com/products/618

  Check out the links above for our tutorials and wiring diagrams.
  These displays use SPI to communicate, 4 or 5 pins are required to
  interface (RST is optional).

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
 **************************************************************************/

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735

//esp8266 nodemcu
// #define TFT_CS         16
// #define TFT_RST        4                                            
// #define TFT_DC         5
//#define TFT_MOSI 0  // Data out
//#define TFT_SCLK 2  // Clock out

//esp32-s2
// #define TFT_CS         5
// #define TFT_RST        9                                            
// #define TFT_DC         7
// #define TFT_MOSI       11  // Data out
// #define TFT_SCLK       12  // Clock out

//esp32-micropython
// cs,rst 排列，屏幕正面，(最左为1) 
//1,2,3,4
#define TFT_CS     18    //   19   18   17   4
#define TFT_RST    23    //   22   23   5    16  

#define TFT_CS1     19    //   19   18   17   4
#define TFT_RST1    22    //   22   23   5    16  

#define TFT_CS2     18    //   19   18   17   4
#define TFT_RST2    23    //   22   23   5    16  

#define TFT_CS3     17    //   19   18   17   4
#define TFT_RST3    5    //   22   23   5    16  

#define TFT_CS4     4    //   19   18   17   4
#define TFT_RST4    16    //   22   23   5    16  


#define TFT_DC         32
#define TFT_MOSI       25  // Data out
#define TFT_SCLK       26  // Clock out
#define TFT_BL 33 //背光
// For 1.44" and 1.8" TFT with ST7735 use:
//Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

// For 1.14", 1.3", 1.54", 1.69", and 2.0" TFT with ST7789:
//Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);


// OPTION 2 lets you interface the display using ANY TWO or THREE PINS,
// tradeoff being that performance is not as fast as hardware SPI above.


// For ST7735-based displays, we will use this call
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS1, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST1);
Adafruit_ST7735 tft2 = Adafruit_ST7735(TFT_CS2, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST2);
Adafruit_ST7735 tft3 = Adafruit_ST7735(TFT_CS3, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST3);
Adafruit_ST7735 tft4 = Adafruit_ST7735(TFT_CS4, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST4);
//Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS1, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST1);
// OR for the ST7789-based displays, we will use this call
//Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);

float p = 3.1415926;

void testlines(Adafruit_ST7735 ptft,uint16_t color) {
  ptft.fillScreen(ST77XX_BLACK);
  for (int16_t x=0; x < ptft.width(); x+=6) {
    ptft.drawLine(0, 0, x, ptft.height()-1, color);
    delay(0);
  }
  for (int16_t y=0; y < ptft.height(); y+=6) {
    ptft.drawLine(0, 0, ptft.width()-1, y, color);
    delay(0);
  }

  ptft.fillScreen(ST77XX_BLACK);
  for (int16_t x=0; x < ptft.width(); x+=6) {
    ptft.drawLine(ptft.width()-1, 0, x, ptft.height()-1, color);
    delay(0);
  }
  for (int16_t y=0; y < ptft.height(); y+=6) {
    ptft.drawLine(ptft.width()-1, 0, 0, y, color);
    delay(0);
  }

  ptft.fillScreen(ST77XX_BLACK);
  for (int16_t x=0; x < ptft.width(); x+=6) {
    ptft.drawLine(0, ptft.height()-1, x, 0, color);
    delay(0);
  }
  for (int16_t y=0; y < ptft.height(); y+=6) {
    ptft.drawLine(0, ptft.height()-1, ptft.width()-1, y, color);
    delay(0);
  }

  ptft.fillScreen(ST77XX_BLACK);
  for (int16_t x=0; x < ptft.width(); x+=6) {
    ptft.drawLine(ptft.width()-1, ptft.height()-1, x, 0, color);
    delay(0);
  }
  for (int16_t y=0; y < ptft.height(); y+=6) {
    ptft.drawLine(ptft.width()-1, ptft.height()-1, 0, y, color);
    delay(0);
  }
}

void testdrawtext(Adafruit_ST7735 ptft,char *text, uint16_t color) {
  ptft.setCursor(0, 0);
  ptft.setTextColor(color);
  ptft.setTextWrap(true);
  ptft.print(text);
}

void testfastlines(Adafruit_ST7735 ptft, uint16_t color1, uint16_t color2) {
  ptft.fillScreen(ST77XX_BLACK);
  for (int16_t y=0; y < ptft.height(); y+=5) {
    ptft.drawFastHLine(0, y, ptft.width(), color1);
  }
  for (int16_t x=0; x < ptft.width(); x+=5) {
    ptft.drawFastVLine(x, 0, ptft.height(), color2);
  }
}

void testdrawrects(Adafruit_ST7735 ptft, uint16_t color) {
  ptft.fillScreen(ST77XX_BLACK);
  for (int16_t x=0; x < ptft.width(); x+=6) {
    ptft.drawRect(ptft.width()/2 -x/2, ptft.height()/2 -x/2 , x, x, color);
  }
}

void testfillrects(Adafruit_ST7735 ptft,uint16_t color1, uint16_t color2) {
  ptft.fillScreen(ST77XX_BLACK);
  for (int16_t x=ptft.width()-1; x > 6; x-=6) {
    ptft.fillRect(ptft.width()/2 -x/2, ptft.height()/2 -x/2 , x, x, color1);
    ptft.drawRect(ptft.width()/2 -x/2, ptft.height()/2 -x/2 , x, x, color2);
  }
}

void testfillcircles(Adafruit_ST7735 ptft,uint8_t radius, uint16_t color) {
  for (int16_t x=radius; x < ptft.width(); x+=radius*2) {
    for (int16_t y=radius; y < ptft.height(); y+=radius*2) {
      ptft.fillCircle(x, y, radius, color);
    }
  }
}

void testdrawcircles(Adafruit_ST7735 ptft,uint8_t radius, uint16_t color) {
  for (int16_t x=0; x < ptft.width()+radius; x+=radius*2) {
    for (int16_t y=0; y < ptft.height()+radius; y+=radius*2) {
      ptft.drawCircle(x, y, radius, color);
    }
  }
}

void testtriangles(Adafruit_ST7735 ptft) {
  ptft.fillScreen(ST77XX_BLACK);
  uint16_t color = 0xF800;
  int t;
  int w = ptft.width()/2;
  int x = ptft.height()-1;
  int y = 0;
  int z = ptft.width();
  for(t = 0 ; t <= 15; t++) {
    ptft.drawTriangle(w, y, y, x, z, x, color);
    x-=4;
    y+=4;
    z-=4;
    color+=100;
  }
}

void testroundrects(Adafruit_ST7735 ptft) {
  ptft.fillScreen(ST77XX_BLACK);
  uint16_t color = 100;
  int i;
  int t;
  for(t = 0 ; t <= 4; t+=1) {
    int x = 0;
    int y = 0;
    int w = ptft.width()-2;
    int h = ptft.height()-2;
    for(i = 0 ; i <= 16; i+=1) {
      ptft.drawRoundRect(x, y, w, h, 5, color);
      x+=2;
      y+=3;
      w-=4;
      h-=6;
      color+=1100;
    }
    color+=100;
  }
}

void tftPrintTest(Adafruit_ST7735 ptft) {
  ptft.setTextWrap(false);
  ptft.fillScreen(ST77XX_BLACK);
  ptft.setCursor(0, 30);
  ptft.setTextColor(ST77XX_RED);
  ptft.setTextSize(1);
  ptft.println("Hello World!");
  ptft.setTextColor(ST77XX_YELLOW);
  ptft.setTextSize(2);
  ptft.println("Hello World!");
  ptft.setTextColor(ST77XX_GREEN);
  ptft.setTextSize(3);
  ptft.println("Hello World!");
  ptft.setTextColor(ST77XX_BLUE);
  ptft.setTextSize(4);
  ptft.print(1234.567);
  delay(1500);
  ptft.setCursor(0, 0);
  ptft.fillScreen(ST77XX_BLACK);
  ptft.setTextColor(ST77XX_WHITE);
  ptft.setTextSize(0);
  ptft.println("Hello World!");
  ptft.setTextSize(1);
  ptft.setTextColor(ST77XX_GREEN);
  ptft.print(p, 6);
  ptft.println(" Want pi?");
  ptft.println(" ");
  ptft.print(8675309, HEX); // print 8,675,309 out in HEX!
  ptft.println(" Print HEX!");
  ptft.println(" ");
  ptft.setTextColor(ST77XX_WHITE);
  ptft.println("Sketch has been");
  ptft.println("running for: ");
  ptft.setTextColor(ST77XX_MAGENTA);
  ptft.print(millis() / 1000);
  ptft.setTextColor(ST77XX_WHITE);
  ptft.print(" seconds.");
}

void mediabuttons() {
  // play
  tft.fillScreen(ST77XX_BLACK);
  tft.fillRoundRect(25, 10, 78, 60, 8, ST77XX_WHITE);
  tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_RED);
  delay(500);
  // pause
  tft.fillRoundRect(25, 90, 78, 60, 8, ST77XX_WHITE);
  tft.fillRoundRect(39, 98, 20, 45, 5, ST77XX_GREEN);
  tft.fillRoundRect(69, 98, 20, 45, 5, ST77XX_GREEN);
  delay(500);
  // play color
  tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_BLUE);
  delay(50);
  // pause color
  tft.fillRoundRect(39, 98, 20, 45, 5, ST77XX_RED);
  tft.fillRoundRect(69, 98, 20, 45, 5, ST77XX_RED);
  // play color
  tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_GREEN);
}
